package com.wyg.blog.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.blog.mapper.MessageMapper;
import com.wyg.blog.model.blog.Message;
import com.wyg.blog.model.blog.MyComment;
import com.wyg.blog.service.blog.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: WYG
 * @time: 2020/7/5 23:05
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    MessageMapper messageMapper;

    @Override
    public IPage<Message> selectSomeMessages(Page<?> page) {
        return messageMapper.getSomeMessages(page);
    }

    @Override
    public List<MyComment<Message>> selectMessagesByParentId(Page<?> page) {
        List<MyComment<Message>> results = new ArrayList<>();
        IPage<Message> messageIPage = messageMapper.getSomeParentMessage(page);
        int total = (int) messageIPage.getTotal();
        int current = (int) messageIPage.getCurrent();
        int size = (int) messageIPage.getSize();
        List<Message> parentMessages = messageIPage.getRecords();
        for (int i = 0; i < parentMessages.size(); i++){
            Message parentMessage = parentMessages.get(i);
            List<Message> childrenMessages = messageMapper.getMessagesByPId(parentMessage.getId());
            MyComment<Message> result = new MyComment<>(parentMessage, childrenMessages);
            result.setTotal(total);
            result.setCurrent(current);
            result.setSize(size);
            results.add(result);
        }
        return results;
    }

    @Override
    public Message selectMessageById(Integer id) {
        return messageMapper.getMessageById(id);
    }

    @Override
    public String selectContentById(Integer id) {
        return messageMapper.getContentById(id);
    }

    @Override
    public boolean insertMessage(Message message) {
        return messageMapper.addMessage(message);
    }

    @Override
    public boolean renewMessageState(Integer id, boolean state) {
        return messageMapper.updateMessageState(id, state);
    }

    @Override
    public boolean renewMessage(Integer id, String content) {
        return messageMapper.updateMessage(id, content);
    }

    @Override
    public boolean removeMessage(Integer id) {
        return messageMapper.deleteMessage(id);
    }

    @Override
    public String selectEmail(Integer id) {
        return messageMapper.getEmailById(id);
    }

    @Override
    public boolean selectState(Integer id) {
        return messageMapper.getStateById(id);
    }
}
